<!DOCTYPE html>
<html lang="en-us">

<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />

  	<meta property="og:title" content=" 绘制验证码 &middot;  我的博客" />
  	<meta property="og:site_name" content="我的博客" />
  	<meta property="og:url" content="/my_technology/yanzhengma/" />
    
    
  	<meta property="og:type" content="article" />

    <meta property="og:article:published_time" content="2020-05-04T21:22:28&#43;08:00" />

    
    

  <title>
     绘制验证码 &middot;  我的博客
  </title>

    <meta name="description" content="这里是付佳康的个人博客" />

    <meta name="HandheldFriendly" content="True" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />

    <link rel="shortcut icon" href="/my_technology/images/favicon.ico">
	  <link rel="apple-touch-icon" href="/my_technology/images/apple-touch-icon.png" />
    
    <link rel="stylesheet" type="text/css" href="/my_technology/css/screen.css" />
    <link rel="stylesheet" type="text/css" href="//fonts.googleapis.com/css?family=Merriweather:300,700,700italic,300italic|Open+Sans:700,400|Inconsolata" />


    
      
          <link href="/my_technology/index.xml" rel="alternate" type="application/rss+xml" title="我的博客" />
      
      
    
    <meta name="generator" content="Hugo 0.68.3" />

    <link rel="canonical" href="/my_technology/yanzhengma/" />

     
</head>
<body class="nav-closed">
<div id="particles-js"></div>
  


 <div class="site-wrapper">



<header class="main-header " style="background-image: url(/my_technology/images/user.jpg)">

    <nav class="main-nav overlay clearfix">
        
            <a class="blog-logo" href="/my_technology/"><img src="/my_technology/images/user.png" alt="Blog Logo" /></a>
        
        
    </nav>
<div class="vertical">
        <div class="main-header-content inner">
            <h1 class="page-title">
              <a class="btn-bootstrap-2" href="#content">我的博客</a>
          </h1>
          <h2 class="page-description">这里是付佳康的个人博客</h2>
        </div>
</div>
    <a class="scroll-down icon-arrow-left" href="#content"><span class="hidden">Scroll Down</span></a>
</header>

  <main id="content" class="content" role="main">


  <article class="post ">

    <header class="post-header">
        <h1 class="post-title">绘制验证码</h1>
        <section class="post-meta">
        
         
        </section>
    </header>

    <section class="post-content">
      <pre><code>验证码：
    #导入图片库
    #绘画库
    from PIL import ImageDraw
    #字体库
    from PIL import ImageFont
    #图片库
    from PIL import Image
    #随机库
    import random
    #文件流
    import io
    # redis
    import redis
    # 定义IP地址和端口
    host = '127.0.0.1'
    port = 6379
    # 定义链接对象
    r = redis.Redis(host=host, port=port)


    # 自定义图片验证码
    class MyCode(View):
        # 定义rgb随机颜色
        def get_random_color(self):
            R = random.randrange(255)
            G = random.randrange(255)
            B = random.randrange(255)
            return (R, G, B)
        # 定义图片视图
        def get(self, request):
            # 画布
            img_size = (120, 50)
            # 定义图片对象
            image = Image.new('RGB', img_size, 'white')
            # 定义画笔
            draw = ImageDraw.Draw(image, 'RGB')
            # 定义随机生成数字
            source = '0123456789asdfghjklzxcvbnmqwertyuiop'
            # 接收容器
            code_str = ''
            # 进入循环绘制， 获取随机字符4个
            for i in range(4):
                # 获取字母颜色
                text_color = self.get_random_color()
                # 获取随机下标
                tmp_num = random.randrange(len(source))
                # 随机字符串
                random_str = source[tmp_num]
                # 装入容器
                code_str += random_str
                # 绘制字符串
                draw.text((10+30*i, 20), random_str, text_color)
            # 获取缓存区 使用io模块
            buf = io.BytesIO()
            # 将临时图片保存到缓存
            image.save(buf, 'png')
            # 保存随机码
            r.set('code', code_str)
            # 保存session
            request.session['code'] = code_str
            print(r.get('code'))
            # 返回图片
            return HttpResponse(buf.getvalue(), 'image/png')

            code = request.GET.get('code', None)


    使用验证码时：
        接参
        code = request.GET.get('code', None)
        获取验证码
        redis_code = r.get('code')
        转码
        redis_code = redis_code.decode('utf-8')

</code></pre>
    </section>


  <footer class="post-footer">


    
    <figure class="author-image">

        <a class="img" href="/my_technology/" style="background-image: url(/my_technology/images/user.png)"><span class="hidden">付佳康</span></a>
    </figure>
    

    <section class="author">

  <p>付佳康</p>
  

</section>


    
    <section class="share">
      <h4>Share this page</h4>
      <a class="icon-twitter" style="font-size: 1.4em" href="https://twitter.com/share?text=%e7%bb%98%e5%88%b6%e9%aa%8c%e8%af%81%e7%a0%81&amp;url=%2fmy_technology%2fyanzhengma%2f"
          onclick="window.open(this.href, 'twitter-share', 'width=550,height=235');return false;">
          <span class="hidden">Twitter</span>
      </a>
      <a class="icon-facebook" style="font-size: 1.4em" href="https://www.facebook.com/sharer/sharer.php?u=%2fmy_technology%2fyanzhengma%2f"
          onclick="window.open(this.href, 'facebook-share','width=580,height=296');return false;">
          <span class="hidden">Facebook</span>
      </a>
      <a class="icon-google-plus" style="font-size: 1.4em" href="https://plus.google.com/share?url=%2fmy_technology%2fyanzhengma%2f"
         onclick="window.open(this.href, 'google-plus-share', 'width=490,height=530');return false;">
          <span class="hidden">Google+</span>
      </a>
    </section>
    

    
    
    

  </footer>
</article>

</main>
    <footer class="site-footer clearfix">
        <section class="copyright"><a href="">我的博客</a> </section>
        
        <section class="poweredby">Proudly generated by <a class="icon-hugo" href="https://gohugo.io">HUGO</a>, with <a class="icon-theme" href="https://github.com/syui/hugo-theme-air">hugo-theme-air</a> theme</section>
        
    </footer>
    </div>
    <script type="text/javascript" src="/my_technology/js/jquery.js"></script>
    <script type="text/javascript" src="/my_technology/js/jquery.fitvids.js"></script>
    <script type="text/javascript" src="/my_technology/js/index.js"></script>
    <script src="/my_technology/js/particles.min.js"></script>
    <script src="/my_technology/js/particles.js"></script>  

</body>
</html>

